package com.ts.api.module.api.convert.core.http.client;

import com.gitee.apanlh.exp.ParamParserException;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.net.http.HttpClientBuilder;
import com.gitee.apanlh.util.net.http.HttpRequestHeader;
import com.gitee.apanlh.util.reflection.ClassTypeUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.gitee.apanlh.web.http.HttpContentType;
import com.gitee.apanlh.web.http.HttpMethod;
import com.ts.api.module.api.convert.core.entity.ApiRequestParameter;
import org.springframework.stereotype.Component;

/**
 *  HTTP请求参数装配器
 *
 *  @author Pan
 */
@Component
public class ApiHttpRequestConfigure {

    /**
     *  请求参数装配
     *
     *  @param  httpMessage     HTTP请求消息对象
     *  @return HttpClientBuilder
     */
    public HttpClientBuilder clientConfigure(ApiHttpMessage httpMessage) {
        String url = httpMessage.getUrl();
        HttpClientBuilder httpClientBuilder;

        if (ValidParam.isEmpty(url)) {
            throw new ParamParserException("分发时请求地址为空,抛出异常");
        }

        //  构建客户端
        if (Eq.enums(HttpContentType.GET, httpMessage.getHttpContentType())) {
            httpClientBuilder = HttpClientBuilder.builder(url, HttpMethod.GET);
        } else {
            httpClientBuilder = HttpClientBuilder.builder(url, HttpMethod.POST);
        }

        //  设置链路id
        if (ValidParam.isNotNull(httpMessage.getReqId())) {
            httpClientBuilder.withLogTraceId(httpMessage.getReqId());
        }

        //  默认设置json请求
        if (ValidParam.isNull(httpMessage.getHttpContentType())) {
            httpMessage.setHttpContentType(HttpContentType.APPLICATION_JSON);
        }

        //  超时时长
        int timeout = httpMessage.getTimeout();
        if (timeout > 0) {
            httpClientBuilder.withReadTimeout(timeout);
            httpClientBuilder.withConnectTimeout(timeout);
        }

        //  关闭urlEncode
        if (httpMessage.getDisableUrlEncode()) {
            httpClientBuilder.disableParamUrlEncode();
        }

        //  header
        HttpRequestHeader httpRequestHeader = httpMessage.getHttpRequestHeader();
        if (ValidParam.isNotNull(httpRequestHeader)) {
            httpClientBuilder.withHeader(httpRequestHeader);
        }

        //  请求体转换
        Object requestBody = httpMessage.getRequestBody();
        //  GET
        if (Eq.enums(HttpContentType.GET, httpMessage.getHttpContentType())) {
            httpClientBuilder.withQueryParams(requestBody);
        }
        //  form
        if (Eq.enums(HttpContentType.APPLICATION_FORM_URLENCODED, httpMessage.getHttpContentType())) {
            httpClientBuilder.withBodyForm(requestBody);
        }
        //  form-data
        if (Eq.enums(HttpContentType.MULTIPART_FORM_DATA, httpMessage.getHttpContentType())) {
            httpClientBuilder.withBodyFormData(requestBody);
        }
        //  json
        if (Eq.enums(HttpContentType.APPLICATION_JSON, httpMessage.getHttpContentType())) {
            if (ClassTypeUtils.isString(requestBody)) {
                httpClientBuilder.withBodyJson(String.valueOf(requestBody));
            } else {
                httpClientBuilder.withBodyJson(requestBody);
            }
        }
        return httpClientBuilder;
    }

    /**
     *  API拓展属性装配
     *
     *  @param  httpMessage         构建客户端
     *  @param  apiRequestParameter   请求消息
     *  @param  requestBody         转化后请求体
     *  @param  reqId               请求唯一id
     *  @param  timeout             请求超时时间
     */
    public void apiExtendConfigure(ApiHttpMessage httpMessage, ApiRequestParameter apiRequestParameter, Object requestBody, String reqId, Integer timeout) {
        //  替换url
        String extendUrl = apiRequestParameter.getExtendUrl();

        if (ValidParam.isNotEmpty(extendUrl)) {
            httpMessage.setUrl(httpMessage.getUrl() + extendUrl);
        }

        httpMessage.setSourceRequestBody(JsonUtils.toJson(apiRequestParameter));
        httpMessage.setRequestBody(requestBody);
        httpMessage.setHttpContentType(apiRequestParameter.getHttpContentType());
        httpMessage.setHttpRequestHeader(apiRequestParameter.getHttpRequestHeader());
        httpMessage.setReqId(Long.valueOf(reqId));
        httpMessage.setTimeout(timeout);
    }
}
